{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "![](https://raw.githubusercontent.com/Qinbf/tf-model-zoo/master/README_IMG/01.jpg)\n",
    "AI MOOC： **www.ai-xlab.com**  \n",
    "如果你也是AI爱好者，可以添加我的微信一起交流：**sdxxqbf**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.86721057 -0.90022621  0.99731177 -0.14064889]\n",
      " [-0.67125645 -0.74732574 -0.07638257  0.48537049]\n",
      " [-0.94840428 -0.17981453  0.42097935  0.82072693]]\n",
      "[[ 0.76036509]\n",
      " [ 0.30174859]\n",
      " [ 0.95224232]\n",
      " [-0.4658122 ]]\n"
     ]
    }
   ],
   "source": [
    "#输入数据\n",
    "X = np.array([[1,0,0],\n",
    "              [1,0,1],\n",
    "              [1,1,0],\n",
    "              [1,1,1]])\n",
    "#标签\n",
    "Y = np.array([[0,1,1,0]])\n",
    "#权值初始化，取值范围-1到1\n",
    "V = np.random.random((3,4))*2-1 \n",
    "W = np.random.random((4,1))*2-1\n",
    "print(V)\n",
    "print(W)\n",
    "#学习率设置\n",
    "lr = 0.11\n",
    "\n",
    "def sigmoid(x):\n",
    "    return 1/(1+np.exp(-x))\n",
    "\n",
    "def dsigmoid(x):\n",
    "    return x*(1-x)\n",
    "\n",
    "def update():\n",
    "    global X,Y,W,V,lr\n",
    "    \n",
    "    L1 = sigmoid(np.dot(X,V))#隐藏层输出(4,4)\n",
    "    L2 = sigmoid(np.dot(L1,W))#输出层输出(4,1)\n",
    "    \n",
    "    L2_delta = (Y.T - L2)*dsigmoid(L2)\n",
    "    L1_delta = L2_delta.dot(W.T)*dsigmoid(L1)\n",
    "    \n",
    "    W_C = lr*L1.T.dot(L2_delta)\n",
    "    V_C = lr*X.T.dot(L1_delta)\n",
    "    \n",
    "    W = W + W_C\n",
    "    V = V + V_C"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Error: 0.0276676876861\n",
      "Error: 0.0271536023241\n",
      "Error: 0.0266652039884\n",
      "Error: 0.0262004747412\n",
      "Error: 0.0257576088499\n",
      "Error: 0.025334985182\n",
      "Error: 0.0249311438386\n",
      "Error: 0.0245447662821\n",
      "Error: 0.0241746583593\n",
      "Error: 0.0238197357366\n",
      "Error: 0.0234790113545\n",
      "Error: 0.0231515845802\n",
      "Error: 0.0228366317942\n",
      "Error: 0.022533398193\n",
      "Error: 0.0222411906277\n",
      "Error: 0.0219593713277\n",
      "Error: 0.0216873523834\n",
      "Error: 0.021424590883\n",
      "Error: 0.0211705846128\n",
      "Error: 0.020924868247\n",
      "Error: 0.020687009962\n",
      "Error: 0.0204566084211\n",
      "Error: 0.0202332900822\n",
      "Error: 0.0200167067896\n",
      "Error: 0.0198065336138\n",
      "Error: 0.0196024669114\n",
      "Error: 0.0194042225782\n",
      "Error: 0.019211534473\n",
      "Error: 0.0190241529943\n",
      "Error: 0.0188418437906\n",
      "Error: 0.0186643865917\n",
      "Error: 0.0184915741469\n",
      "Error: 0.0183232112587\n",
      "Error: 0.0181591139024\n",
      "Error: 0.0179991084232\n",
      "Error: 0.0178430308014\n",
      "Error: 0.0176907259808\n",
      "Error: 0.0175420472525\n",
      "Error: 0.01739685569\n",
      "Error: 0.0172550196301\n",
      "[[ 0.01737005]\n",
      " [ 0.98343067]\n",
      " [ 0.98346612]\n",
      " [ 0.01799349]]\n",
      "0\n",
      "1\n",
      "1\n",
      "0\n"
     ]
    }
   ],
   "source": [
    "for i in range(20000):\n",
    "    update()#更新权值\n",
    "    if i%500==0:\n",
    "        L1 = sigmoid(np.dot(X,V))#隐藏层输出(4,4)\n",
    "        L2 = sigmoid(np.dot(L1,W))#输出层输出(4,1)\n",
    "        print('Error:',np.mean(np.abs(Y.T-L2)))\n",
    "        \n",
    "L1 = sigmoid(np.dot(X,V))#隐藏层输出(4,4)\n",
    "L2 = sigmoid(np.dot(L1,W))#输出层输出(4,1)\n",
    "print(L2)\n",
    "\n",
    "def judge(x):\n",
    "    if x>=0.5:\n",
    "        return 1\n",
    "    else:\n",
    "        return 0\n",
    "\n",
    "for i in map(judge,L2):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
